package com.frejoys.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frejoys.common.aop.Authority;
import com.frejoys.common.aop.Authorization;
import com.frejoys.common.constant.CommonConstant;
import com.frejoys.common.dto.admin.*;
import com.frejoys.common.dto.common.IdDto;
import com.frejoys.common.error.HttpStatus;
import com.frejoys.common.exception.GeneralException;
import com.frejoys.common.response.Result;
import com.frejoys.common.util.CacheKeyUtil;
import com.frejoys.common.util.RequestUtil;
import com.frejoys.common.vo.admin.AdminCategoryInfoVo;
import com.frejoys.common.vo.admin.AdminCategoryPageVo;
import com.frejoys.dao.entity.Category;
import com.frejoys.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "品类管理", description = "品类管理")
@RestController
@RequiredArgsConstructor
@RequestMapping(CommonConstant.routePrefixAdmin)
@Authorization(Authority.Private)
public class AdminCategoryController {


    private final CategoryService categoryService;

    private final RedissonClient redissonClient;


//    @Operation(summary = "品类列表")
//    @PostMapping("/category/list")
//    public Result<List<CategorySelectVo>> storeList() {
//        return Result.success(categoryService.getSelectVo());
//    }

    @Operation(summary = "添加品类")
    @PostMapping("/category/addInfo")
    public Result addInfo(@RequestBody @Validated CategoryAddDto dto) {
        List<Category> categories = categoryService.listInfo();
        if (categories.size() > 20){
            throw new GeneralException(HttpStatus.categoryNotExceedTwenty);
        }
        RLock lock = redissonClient.getLock(CacheKeyUtil.addCategoryLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {

                categoryService.addInfo(dto);

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }

    @Operation(summary = "编辑品类")
    @PostMapping("/category/upInfo")
    public Result upInfo(@RequestBody @Validated CategoryUpDto dto) {
        RLock lock = redissonClient.getLock(CacheKeyUtil.upCategoryLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {

                categoryService.upInfo(dto);

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }


    @Operation(summary = "删除品类")
    @PostMapping("/category/delInfo")
    public Result delInfo(@RequestBody @Validated DelIdDto dto) {
        RLock lock = redissonClient.getLock(CacheKeyUtil.upCategoryLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {

                categoryService.delInfo(dto);

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }

        return Result.success();
    }


    /**
     * 后台查询品类列表
     */
    @Operation(summary = "后台查询品类列表")
    @PostMapping("/category/page")
        public Result<IPage<AdminCategoryPageVo>> pageCategory(@RequestBody @Validated CategoryPageDto dto) {
        return Result.success(categoryService.pageCategory(dto));
    }

    /**
     * 后台查询类目详情
     */
    @Operation(summary = "后台查询类目详情")
    @PostMapping("/category/getInfo")
    public Result<AdminCategoryInfoVo> getInfo(@RequestBody @Validated IdDto dto) {
        return Result.success(categoryService.getInfo(dto));
    }


    /**
     * 后台修改类目排序
     */
    @Operation(summary = "后台修改类目排序")
    @PostMapping("/category/changeSort")
    public Result changeSort(@RequestBody @Validated CategoryChangeSortDto dto) {
        RLock lock = redissonClient.getLock(CacheKeyUtil.upCategoryLockCacheKey(RequestUtil.userId()));

        if (lock.tryLock()) {
            try {

                categoryService.changeSort(dto);

            } finally {
                if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                    lock.unlock();
                }
            }
        } else {
            throw new GeneralException(HttpStatus.beingProcessed);
        }
        return Result.success();
    }


}
